package com.test.daily.leetcode.y2022.m06.day0606.v03;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Stack;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/6/6 下午 11:39
 * @version: 1.0
 */
public class Solution {
    public static void print(String s) {
        char[] str = s.toCharArray();
        ArrayList<Character> strList = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            strList.add(str[i]);
        }
        ArrayList<String> ans = new ArrayList<>();
        process(strList, ans, "");
    }

    public static void process(ArrayList<Character> rest, ArrayList<String> ans, String pre) {
        if (rest.size() == 0) {
            ans.add(pre);
        } else {
            for (int i = 0; i < rest.size(); i++) {
                char cur = rest.get(i);
                rest.remove(i);
                process(rest, ans, pre + cur);
                rest.add(i, cur);
            }
        }
    }

    // 打印所有子串
    public static void printAllSubStr(String s) {
        char[] str = s.toCharArray();
        String path = "";
        ArrayList<String> ans = new ArrayList<>();
        process(str, 0, path, ans);
        HashSet<String> sets = new HashSet<>();
        for (String ele : ans) {
            sets.add(ele);
        }
    }

    public static void process(char[] str, int index, String path, ArrayList<String> ans) {
        if (index == str.length) {
            ans.add(path);
            return;
        }
        //
        process(str, index + 1, path + String.valueOf(str[index]), ans);
        //
        process(str, index + 1, path, ans);

    }

    public static void printAllSequece(String s) {
        char[] str = s.toCharArray();
        ArrayList<Character> originalList = new ArrayList<>();
        for (Character ch : str) {
            originalList.add(ch);
        }
        ArrayList<String> ans = new ArrayList<>();
        process1(originalList, "", ans);

    }

    public static void process1(ArrayList<Character> rest, String pre, ArrayList<String> ans) {
        if (rest.size() == 0) {
            ans.add(pre);
        } else {
            for (int i = 0; i < rest.size(); i++) {
                char cur = rest.get(i);
                rest.remove(i);
                process1(rest, pre + cur, ans);
                rest.add(i, cur);
            }
        }
    }

    public static void printAllPre(String s) {
        char[] str = s.toCharArray();
        ArrayList<String> ans = new ArrayList<>();
        boolean[] visited = new boolean[26];
    }

    public static void process2(char[] str, int index, ArrayList<String> ans, boolean[] visited) {
        if (index == str.length) {
            ans.add(String.valueOf(str));
            return;
        }
        for (int i = index; i < str.length; i++) {
            if (!visited[str[i] - 'a']) {
                swap(str, index, i);
                visited[str[i] - 'a'] = true;
                process2(str, i + 1, ans, visited);
                swap(str, i, index);
            }

        }
    }

    public static void swap(char[] str, int i, int j) {
        char tmp = str[i];
        str[i] = str[j];
        str[j] = tmp;
    }

    public void reverse(Stack<Integer> stack) {
        int i = stack.pop();
        reverse(stack);
        stack.push(i);
    }

    public int f(Stack<Integer> stack) {
        int result = stack.pop();
        if (stack.isEmpty()) {
            return result;
        } else {
            int last = f(stack);
            stack.push(result);
            return last;
        }
    }
}
